//导入数据库
const db=require('../db/index')
//导入bcryptjs
const bcrypt=require('bcryptjs')
const moment=require('moment')

exports.doctorall=(req,res)=>{
    const sql='select * from doctor'
    db.query(sql,(err,results)=>{
        if(err) return res.cc(err)
        res.json(results)
    })
}

exports.find=(req,res)=>{
    var info=req.body
    var type=info.selecttype
    var sql=` select * from doctor `
    if(info.inputtext.length!=0){
        var name=info.inputtext
        sql+=` where `
        if(type=='doc_name')
        {
            sql+=` doc_name like '%${name}%' `
        }else if(type=='doc_email')
        {
            sql+=` doc_email = '${name}' `
        }else if(type=='doc_tel')
        {
            sql+=` doc_tel = '${name}' `
        }
    }
    
    if(info.start_date.length!=0||info.end_date.length!=0)
    {
           
    var start_date=moment(info.start_date).format('YYYY-MM-DD')
    var end_date=moment(info.end_date).format('YYYY-MM-DD')
        if(info.inputtext.length!=0){
            sql+=` and `
        }else if(info.inputtext.length==0)
        {
            sql+=` where `
        }
        if(info.start_date.length!=0&&info.end_date.length!=0){
            sql+=` doc_hiredate >= '${start_date}' and doc_hiredate <= '${end_date}' `
        }else if(info.start_date.length!=0){
            sql+=` doc_hiredate >= '${start_date}' `
        }else if(info.end_date.length!=0){
            sql+=` doc_hiredate <= '${end_date}' `
        }
    }
    console.log(sql)
    db.query(sql,(err,results)=>{
        if(err) return res.cc(err)
        res.json(results)
    })
}


exports.doctordelete=(req,res)=>{
    const doctorinfo=req.body
    const sql='delete from doctor where doc_id=?'
    db.query(sql,doctorinfo.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1) return res.cc('failed to delete')
        res.cc('delete successfully',0)
    })
}
exports.doctorupdate=(req,res)=>{
    const doctorinfo=req.body
    const date=moment(doctorinfo.hiredate).format('YYYY-MM-DD')
    const sql='update doctor set ? where doc_id=?'
    db.query(sql,[{doc_name:doctorinfo.username,doc_email:doctorinfo.email,doc_tel:doctorinfo.tel,doc_hiredate:date},doctorinfo.id],(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to update')
        res.cc('update successful',0)
    })
}
exports.doctorreset=(req,res)=>{
    const doctorinfo=req.body
    doctorinfo.password=bcrypt.hashSync(doctorinfo.password,10)
    const sql='update doctor set ? where doc_id=?'
    db.query(sql,[{doc_password:doctorinfo.password},doctorinfo.id],(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to reset')
        res.cc('reset successful',0)
    })
}

exports.doctorinsert=(req,res)=>{
    const doctorinfo=req.body
    doctorinfo.password=bcrypt.hashSync(doctorinfo.password,10)
    const date=moment(doctorinfo.hiredate).format('YYYY-MM-DD')
    const sql='select * from doctor where doc_email=?'
    db.query(sql,doctorinfo.username,(err,results)=>{
        if(err)return res.cc(err)
        if(results.length==1) res.cc('email address is exist,please change and try again')
        const sql='insert into doctor set ?'
        db.query(sql,{doc_name:doctorinfo.username,doc_password:doctorinfo.password,doc_email:doctorinfo.email,doc_tel:doctorinfo.tel,doc_hiredate:date},(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to insert')
        res.cc('insert successful',0)
    })
    })  
}

